var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var conf = require('./config');
var path = require('path');


module.exports = function(){
    console.log('init express .....');

    var app = express();

    app.use(bodyParser.json());

    //前端路由根目录
    var rootPath = path.resolve('public');

    //session登录控制
    app.use(cookieParser('sctalk'));
    app.use(session({
        secret:'Kevin-Tan-Secret-User',
        rolling:true,
        cookie:{maxAge:3000*1000} //5分钟过期
    }));

    //登录拦截器
    var strRegex = '('+conf.suffix+')$'; //用于验证url是否包含特定后缀名
    var suffixReg=new RegExp(strRegex);
    app.use(function (req, res, next) {
        var url = req.originalUrl;
        if (url != "/login" && url!="/getByUser" && !req.session.user) {
            if (suffixReg.test(url.toLowerCase())) {
                //过滤拦截的Url
            } else {
              return res.redirect("/login");
            }
        }
        next();
    });

    //静态资源文件指向
    app.use(express.static(rootPath));

    //后台路由
    require('../app/routes/honor.server.routes')(app);
    require('../app/routes/user.server.routes')(app);
    require('../app/routes/index.server.routes')(app);
    require('../app/routes/mortgate.server.routes')(app); //抵押贷
    require('../app/routes/assist.server.routes')(app); //辅助数据操作
    require('../app/routes/monthly.server.routes')(app); //月供贷
    require('../app/routes/warranty.server.routes')(app); //保单贷
    require('../app/routes/payroll.server.routes')(app); //工薪贷
    require('../app/routes/social.server.routes')(app); //社保贷
    require('../app/routes/decorate.server.routes')(app); //装修贷

    app.set('views',rootPath+'/views');
    app.set('view engine','ejs');
    require(rootPath+'/routes/user.client.route')(app); //用户相关页面
    require(rootPath+'/routes/mortgate.client.route')(app); //抵押贷款页面
    require(rootPath+'/routes/monthly.client.route')(app); //月供贷款页面
    require(rootPath+'/routes/warranty.client.route')(app); //保单贷款页面
    require(rootPath+'/routes/payroll.client.route')(app); //工薪贷款页面
    require(rootPath+'/routes/social.client.route')(app); //社保贷款页面
    require(rootPath+'/routes/decorate.client.route')(app); //装修贷款页面
    require(rootPath+'/routes/assist.client.route')(app); //辅助数据管理页面
    require(rootPath+'/routes/honor.client.route')(app); //

    app.use(function(req , res , next){
        res.status(404);
        try{
            return res.json('Not Found');
        }catch(e){
            console.log('404 set header after sent');
        }
    });


    app.use(function(err , req , res , next){
        if(!err){
            return next();
        }
        res.status(500);
        try{
            return res.json(err.message || 'server error');
        }catch(e){
            console.log('500 set header after sent');
        }
    });


    return app;
};